codeforces-1015c Songs Compression(优先队列)

描述

传送门:Songs Compression

Ivan has n songs on his phone. The size of the i-th song is ai bytes. Ivan also has a flash drive which can hold at most m bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all n songs to the flash drive. He can compress the songs. If he compresses the i-th song, the size of the i-th song reduces from ai to bi bytes (bi<ai).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most m. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to m).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print “-1”. Otherwise print the minimum number of songs Ivan needs to compress.

输入描述

The first line of the input contains two integers n and m (1≤n≤105,1≤m≤109) — the number of the songs on Ivan’s phone and the capacity of Ivan’s flash drive.

The next n lines contain two integers each: the i-th line contains two integers ai and bi (1≤ai,bi≤109, ai>bi) — the initial size of the i-th song and the size of the i-th song after compression.

输出描述

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print “-1”. Otherwise print the minimum number of the songs to compress.

示例

输入

1
2
3
4
5
6
7
8
9
10
4 21
10 8
7 4
3 1
5 4
4 16
10 8
7 4
3 1
5 4

输出

1
2
2
-1

Hint

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to 8+7+1+5=21≤21. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal 8+4+3+5=20≤21. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to 10+4+3+5=22>21).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal 8+4+1+4=17>16.

题解

题目大意

给出 n 首歌曲压缩前与压缩后的大小,以及优盘的空间 m,求最少压缩几首歌能将所有歌放入优盘,如果放不进去输出 -1

思路

贪心,先判断所有歌曲压缩后的大小是否小于m,如果大于,则说明放不进去,输出 -1,然后再将所有歌曲按照可压缩空间的大小排序,不断计算压缩值统计压缩数量即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int MAXN = 105;
using namespace std;
ll sum;

int main(){
int n, m;
while(cin >> n >> m){
priority_queue<int, vector<int>, greater<int> > que;
sum = 0;
while(n--){
int a, b;
cin >> a >> b;
sum += a;
que.push(b-a);
}
int ans = 0;
while(!que.empty()){
if(sum <= m){
break;
}
else{
ans++;
sum += que.top();
que.pop();
}
}
if(sum <= m){
cout << ans << endl;
}
else{
cout << "-1" << endl;
}
}
}